/**
 * Created by zzy on 2017/2/9.
 */
var codeType = {"B":"包裹码","C":"托盘"};
require(['jquery','require','page/layout/util',"libs/alert",'easyui',"libs/confirm","bootstrap"],function(){
    $(".header").show();
    $.parser.parse();
    //获得url参数
    var loadInfoId = $.getUrlParam("loadInfoId");
    //物流承运商判断
    var logisticsProviderIdTemp = "";
    //var loadInfoId = $.getUrlParam('loadInfoId');
    tableInit([]);
    dataInit(loadInfoId);
    $("#create").on("click",function () {
        var rows = $('#main_table').datagrid('getSelections');
        var boxCodes = [];
        var flag = true;
         if (rows.length > 0) {
             $.each(rows,function (index,row) {
                 boxCodes.push(row.code);

                 if(logisticsProviderIdTemp != "" && row.logisticsProviderId != logisticsProviderIdTemp){
                     alert("不是同一个物流承运商，请重新选择！");
                     flag = false;
                     return;
                 }
                 logisticsProviderIdTemp = row.logisticsProviderId;
             })
         } else {
            alert("请选择一条记录！");
            return;
         }
         if (flag){
             var obj = {};
             obj["loadId"]=loadInfoId;
             obj["logisticsProviderId"]=logisticsProviderIdTemp;
             obj["boxCodes"]=boxCodes;
             createShip(obj)
         }

    })
});
function tableInit(data) {
    $("#main_table").datagrid({
        data: data,
        border : false,
        idField : 'code',
        rownumbers: true,
        nowrap: false,
        frozenColumns:[[{
            field : 'codeId',
            checkbox:true
        }]],
        columns:[[ {
            field : 'code',
            title : '码信息'
        },{
            field:'codeType',
            title:'码类型',
            formatter:function (value,row,index) {
                return codeType[value];
            }
        },{
            field:'orderNo',
            title:'所属订单'
        },{
            field:'logisticsProviderName',
            title:'物流承运商'
        },{
            field:'logisticsProviderId',
            title:'物流承运商id',
            hidden: true
        }
        ]],
        onClickRow: function (rowIndex, rowData) {
            $(this).datagrid('unselectRow', rowIndex);
        }
    });
}
function dataInit(loadInfoId){
    $.ajax({
        url:'api/v1/p/mms/loadInfo/queryOutroomBox',
        data:{
            id:loadInfoId
        }
    }).done(function (result) {
        if(result.code == '200'){
            var data = result.data;
            $("#main_table").datagrid('loadData',data);
        }else{
            return result.message;
        }
    })
}
/* 调后台新增入库单接口*/
function createShip(obj){
    $.ajax({
        url:'api/v1/p/wms/shpping/note/create',
        type:'post',
        data: JSON.stringify(obj),
        contentType: "application/json; charset=utf-8",
        dataType:'json',
        success:function (result) {
            if(result.code == 200){
                window.location.href="load-info-list";
            }else{
                alert(result.message);
            }
        }
    })
}
